home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicUtilities.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  287 lines

  1. /*
  2.  * @(#)OrganicUtilities.java    1.5 98/02/04
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.plaf.*;
  25. import java.awt.Color;
  26. import java.awt.Dimension;
  27. import java.awt.Font;
  28. import java.awt.FontMetrics;
  29. import java.awt.Graphics;
  30. import java.awt.Insets;
  31. import java.awt.Rectangle;
  32. import java.awt.event.KeyEvent;
  33. import com.sun.java.swing.plaf.basic.BasicGraphicsUtils;
  34.  
  35.  
  36. /*
  37.  * @version 1.5 02/04/98
  38.  * @author Michael C. Albers
  39.  */
  40. public class OrganicUtilities
  41. {
  42.     /** Draw a string with the graphics g at location (x,y) just like g.drawString() would.
  43.      *  The first occurence of underlineChar in text will be underlined. The matching is
  44.      *  not case sensitive.
  45.      */
  46.   public static void drawString(Graphics g,String text,
  47.                 int underlinedChar,int x,int y) {
  48.  
  49.     char b[] = new char[1];
  50.     String s;
  51.     char lc,uc;
  52.     int index=-1,lci,uci;
  53.     
  54.     if(underlinedChar != '\0') {
  55.       b[0] = (char)underlinedChar;
  56.       s = new String(b).toUpperCase();
  57.       uc = s.charAt(0);
  58.       
  59.       s = new String(b).toLowerCase();
  60.       lc = s.charAt(0);
  61.       
  62.       uci = text.indexOf(uc);
  63.       lci = text.indexOf(lc);
  64.       
  65.       if(uci == -1)
  66.     index = lci;
  67.       else if(lci == -1)
  68.     index = uci;
  69.       else
  70.     index = (lci < uci) ? lci : uci;
  71.     }
  72.     
  73.     g.drawString(text,x,y);
  74.     if(index != -1) {
  75.       FontMetrics fm = g.getFontMetrics();
  76.       Rectangle underlineRect = new Rectangle();
  77.       underlineRect.x = x + fm.stringWidth(text.substring(0,index));
  78.       underlineRect.y = y;
  79.       underlineRect.width = fm.charWidth(text.charAt(index));
  80.       underlineRect.height = 1;
  81.       g.fillRect(underlineRect.x,underlineRect.y + fm.getDescent() - 1,
  82.          underlineRect.width,underlineRect.height);
  83.     }
  84.   }
  85.   
  86.  
  87.   
  88.   public static void drawDashedRect(Graphics g,int x,int y,int width,int height) {
  89.     int vx,vy;
  90.     
  91.     // draw upper and lower horizontal dashes
  92.     for (vx = x; vx < (x + width); vx+=2) {
  93.       g.drawLine(vx, y, vx, y);
  94.       g.drawLine(vx, y + height-1, vx, y + height-1);
  95.     }
  96.     
  97.     // draw left and right vertical dashes
  98.     for (vy = y; vy < (y + height); vy+=2) {
  99.       g.drawLine(x, vy, x, vy);
  100.       g.drawLine(x+width-1, vy, x + width-1, vy);
  101.     }
  102.   }
  103.  
  104.     public static void paintMenuItem(Graphics g, JComponent c,
  105.                      Icon checkIcon, Icon arrowIcon,
  106.                      Color background, Color foreground,
  107.                      int defaultTextIconGap)
  108.     {
  109.         JMenuItem b = (JMenuItem) c;
  110.         ButtonModel model = b.getModel();
  111.  
  112.         Dimension size = b.getSize();
  113.  
  114.     Insets i = c.getInsets();
  115.  
  116.         Rectangle viewRect = new Rectangle(size);
  117.  
  118.     viewRect.x += i.left;
  119.     viewRect.y += i.top;
  120.     viewRect.width -= (i.right + viewRect.x);
  121.     viewRect.height -= (i.bottom + viewRect.y);
  122.  
  123.         Rectangle iconRect = new Rectangle();
  124.         Rectangle textRect = new Rectangle();
  125.         Rectangle acceleratorRect = new Rectangle();
  126.         Rectangle checkRect = new Rectangle();
  127.         Rectangle arrowRect = new Rectangle();
  128.  
  129.     Font holdf = g.getFont();
  130.     Font f = c.getFont();
  131.     g.setFont( f );
  132.         FontMetrics fm = g.getFontMetrics( f );
  133.     FontMetrics fmAccel = g.getFontMetrics( UIManager.getFont("MenuItem.acceleratorFont") );
  134.  
  135.     // Paint background
  136.     Color holdc = g.getColor();
  137.  
  138.     g.setColor( b.getBackground() );
  139.     g.fillRect( 0, 0, size.width, size.height );
  140.  
  141.     if ( model.isArmed() || ( c instanceof JMenu && model.isSelected() ) )
  142.     {
  143.         g.setColor( background );
  144.  
  145.         if ( c.getParent() instanceof JMenuBar )
  146.         {
  147.           g.fillRect(0,0, size.width, size.height);
  148.         }
  149.         else
  150.         {
  151.           g.fillRect( 2, 2, size.width - 4, size.height - 4 );
  152.         }
  153.     }
  154.  
  155.     // get Accelerator text
  156.     KeyStroke accelerator =  b.getAccelerator();
  157.     String acceleratorText = "";
  158.     if (accelerator != null) {
  159.         int modifiers = accelerator.getModifiers();
  160.         if (modifiers > 0) {
  161.         acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
  162.         acceleratorText += "+";
  163.       }
  164.         acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode());
  165.     }
  166.  
  167.     // layout the text and icon
  168.         String text = BasicGraphicsUtils.layoutMenuItem(
  169.         fm, b.getText(), fmAccel, acceleratorText, b.getIcon(),
  170.         checkIcon, arrowIcon,
  171.         b.getVerticalAlignment(), b.getHorizontalAlignment(),
  172.         b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  173.         viewRect, iconRect, textRect, 
  174.         acceleratorRect, checkRect, arrowRect,
  175.         b.getText() == null ? 0 : defaultTextIconGap,
  176.         defaultTextIconGap
  177.     );
  178.       
  179.     // Paint the Check
  180.     if (checkIcon != null) {
  181.         if(model.isArmed() || (c instanceof JMenu && model.isSelected())) {
  182.         g.setColor(foreground);
  183.         } else {
  184.         g.setColor(b.getForeground());
  185.         }
  186.         checkIcon.paintIcon(c, g, checkRect.x, checkRect.y);
  187.         g.setColor(holdc);
  188.     }
  189.  
  190.     // Paint the Icon
  191.         if(b.getIcon() != null) { 
  192.             Icon icon;
  193.             if(!model.isEnabled()) {
  194.                 icon = (Icon) b.getDisabledIcon();
  195.             } else if(model.isPressed() && model.isArmed()) {
  196.                 icon = (Icon) b.getPressedIcon();
  197.                 if(icon == null) {
  198.                     // Use default icon
  199.                     icon = (Icon) b.getIcon();
  200.                 } 
  201.             } else {
  202.                 icon = (Icon) b.getIcon();
  203.             }
  204.           
  205.            
  206.         icon.paintIcon(c, g, iconRect.x, iconRect.y);
  207.         }
  208.  
  209.     // Draw the Text
  210.         if ( text != null && !text.equals("") )
  211.     {
  212.             if ( !model.isEnabled() )
  213.         {
  214.                 // *** paint the text disabled
  215.                 g.setColor( UIManager.getColor("MenuItem.disabledForeground") );
  216.                 BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  217.                                               textRect.x, textRect.y + fm.getAscent());
  218.             }
  219.         else
  220.         {
  221.                 // *** paint the text normally
  222.         if ( model.isArmed() || ( c instanceof JMenu && model.isSelected() ) )
  223.         {
  224.             if ( c.getParent() instanceof JMenuBar )
  225.             {
  226.             g.setColor( UIManager.getColor("Menu.pressedForeground") );
  227.             }
  228.             else
  229.             {
  230.             g.setColor( UIManager.getColor("MenuItem.pressedForeground") );
  231.             }
  232.         }
  233.         else
  234.         {
  235.             if ( c.getParent() instanceof JMenuBar )
  236.             {
  237.             g.setColor( UIManager.getColor("Menu.foreground") );
  238.             }
  239.             else
  240.             {
  241.             g.setColor( UIManager.getColor("MenuItem.foreground") );
  242.             }
  243.         }
  244.  
  245.                 BasicGraphicsUtils.drawString(g,text, 
  246.                           model.getMnemonic(),
  247.                                               textRect.x,
  248.                                               textRect.y + fm.getAscent());
  249.             }
  250.         }
  251.       
  252.     // Draw the Accelerator Text
  253.         if(acceleratorText != null && !acceleratorText.equals("")) {
  254.         g.setFont( UIManager.getFont("MenuItem.acceleratorFont") );
  255.             if(!model.isEnabled()) {
  256.                 // *** paint the acceleratorText disabled
  257.                 g.setColor( UIManager.getColor("MenuItem.disabledForeground") );
  258.                 BasicGraphicsUtils.drawString(g,acceleratorText,model.getMnemonic(),
  259.                                               acceleratorRect.x, acceleratorRect.y + fm.getAscent());
  260.             } else {
  261.                 // *** paint the acceleratorText normally
  262.         if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) {
  263.             g.setColor( UIManager.getColor("MenuItem.acceleratorPressedForeground") );
  264.         } else {
  265.             g.setColor( UIManager.getColor("MenuItem.acceleratorForeground") );
  266.         }
  267.                 BasicGraphicsUtils.drawString(g,acceleratorText, 
  268.                           model.getMnemonic(),
  269.                                               acceleratorRect.x,
  270.                                               acceleratorRect.y + fm.getAscent());
  271.             }
  272.         }
  273.  
  274.     // Paint the Arrow
  275.     if (arrowIcon != null) {
  276.         if(model.isArmed() || (c instanceof JMenu &&model.isSelected()))
  277.         g.setColor(foreground);
  278.         if( !(b.getParent() instanceof JMenuBar) )
  279.         arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y);
  280.     }
  281.     g.setColor(holdc);
  282.     g.setFont(holdf);
  283.     }
  284.  
  285. }
  286.  
  287.